package ie.dkit.semanticPhotoWeb.client.panels;

import java.util.List;

import ie.dkit.semanticPhotoWeb.client.ClientServerConnectorAsync;
import ie.dkit.semanticPhotoWeb.client.dtos.AlbumDTO;
import ie.dkit.semanticPhotoWeb.client.dtos.PersonDTO;
import ie.dkit.semanticPhotoWeb.client.panels.tag.TagPanel;
import ie.dkit.semanticPhotoWeb.client.tools.AsyncCallbackAdapter;
import ie.dkit.semanticPhotoWeb.client.tools.ImageHyperlink;
import ie.dkit.semanticPhotoWeb.client.tools.LoginListener;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.layout.VerticalLayout;

/**
 * @author Sebastian Ruehl
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
public class UserOverviewPanel extends Panel implements LoginListener {

	private class AlbumCallBack extends AsyncCallbackAdapter {

		public void onSuccess(Object arg0) {
			List list = (List) arg0;
			HorizontalPanel hp = new HorizontalPanel();
			hp.setSpacing(4);
			add(hp);
			for (int i = 0; i < list.size(); i++) {
				if (i % 4 == 0) {
					(hp = new HorizontalPanel()).setSpacing(4);
					add(hp);
				}
				hp.add(new AlbumIcon((AlbumDTO) list.get(i)));
			}
			add(m_RDFFOAFPanel);
			MessageBox.hide();
			doLayout();
		}

	}

	private class UserProfileCallBack implements AsyncCallback {
		public void onFailure(Throwable caught) {
			m_CurrentPersonDTO = null;
			m_RDFLink.setHTML("No RDF- profile available");
		}

		public void onSuccess(Object result) {
			m_CurrentPersonDTO = (PersonDTO) result;
			m_RDFLink.setHTML("<a href=\"" + m_CurrentPersonDTO.rdfFile
					+ "\" type= \"application/rdf+xml\">RDF-Profile</a>");
			for (int i = 0; i < m_CurrentPersonDTO.knows.length; i++) {
				if (i == 0) {
					m_RDFFOAFPanel.add(new HTML("Knows following Persons:"));
				}
				m_RDFFOAFPanel.add(new Hyperlink(
						m_CurrentPersonDTO.knows[i].username,
						m_CurrentPersonDTO.knows[i].username));
			}
			m_RDFFOAFPanel.doLayout();
		}
	}

	private class AlbumIconListener implements ClickListener {

		private AlbumDTO albumIconDTO;

		public AlbumIconListener(AlbumDTO albumIconDTO) {
			this.albumIconDTO = albumIconDTO;
		}

		public void onClick(Widget sender) {
			m_centerPanel.addAlbumPanel(albumIconDTO);
		}

	}

	private class AlbumIcon extends Panel {
		public AlbumIcon(AlbumDTO dto) {
			ImageHyperlink link = new ImageHyperlink(new Image(dto.iconUrl));
			link.addClickListener(new AlbumIconListener(dto));
			setTitle(dto.albumName);
			add(link);
			add(new HTML("<font size=\"-2\">" + dto.date + "</font>"));
		}
	}

	private ClientServerConnectorAsync m_ClientServerConnectorAsync;

	private CenterPanel m_centerPanel;

	private String m_CurrentUsername;

	private Panel m_RDFFOAFPanel;

	private HTML m_RDFLink;

	private PersonDTO m_CurrentPersonDTO;

	private Button m_KnowThisPerson;

	private boolean loggedIn;

	public UserOverviewPanel(
			ClientServerConnectorAsync clientServerConnectorAsync,
			CenterPanel centerPanel, boolean loggedIn) {
		this(clientServerConnectorAsync, centerPanel, null, loggedIn);
	}

	public UserOverviewPanel(
			ClientServerConnectorAsync clientServerConnectorAsync,
			CenterPanel centerPanel, String username, boolean loggedIn) {
		m_CurrentUsername = username;
		m_ClientServerConnectorAsync = clientServerConnectorAsync;
		m_centerPanel = centerPanel;
		this.loggedIn = loggedIn;
		setAutoScroll(true);
		{
			m_RDFFOAFPanel = new Panel("Friend-of-a-Friend");
			m_RDFFOAFPanel.setLayout(new VerticalLayout());
			m_RDFFOAFPanel.add(m_RDFLink = new HTML());
			m_RDFFOAFPanel.setCollapsible(true);
		}
		if (username != null) {
			setTitle("Albums from: " + username);
			setClosable(true);
			loadAlbumIcons(username);
		} else {
			setTitle("Overview");
			setClosable(false);
			add(new WelcomePage());
		}
	}

	public void loadAlbumIcons() {
		loadAlbumIcons(null);
	}

	private void loadAlbumIcons(String username) {
		clear();
		add(new TagPanel(m_ClientServerConnectorAsync, this));
		MessageBox.wait("Please wait... fetching Albums");
		if (username == null) {
			m_ClientServerConnectorAsync
					.getUserProfile(new UserProfileCallBack());
			m_ClientServerConnectorAsync.getAlbums(new AlbumCallBack());
		} else {
			m_RDFFOAFPanel.add(m_KnowThisPerson = new Button(
					"I know this Person", new ButtonListenerAdapter() {
						public void onClick(Button button, EventObject e) {
							m_ClientServerConnectorAsync.addKnowRelation(
									m_CurrentPersonDTO,
									new AsyncCallbackAdapter() {
										public void onSuccess(Object result) {
											MessageBox
													.alert("Added to known Persons");
											m_KnowThisPerson.disable();
										}
									});
						}
					}));
			if (loggedIn) {
				m_KnowThisPerson.enable();
			} else {
				m_KnowThisPerson.disable();
			}
			m_ClientServerConnectorAsync.getUserProfileByUserName(username,
					new UserProfileCallBack());
			m_ClientServerConnectorAsync.getAlbums(username,
					new AlbumCallBack());
		}
	}

	/**
	 * Return the current username of the panel
	 * @return
	 */
	public String getCurrentUsername() {
		return m_CurrentUsername;
	}

	/**
	 * Refreshs the Panel
	 *
	 */
	public void refresh() {
		loadAlbumIcons(m_CurrentUsername);
	}

	/**
	 * shows the WelcomePage
	 *
	 */
	public void showWelcomePage() {
		clear();
		add(new WelcomePage());
		doLayout();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ie.dkit.semanticPhotoWeb.client.tools.LoginListener#loggedIn(java.lang.String)
	 */
	public void loggedIn(String username) {
		loggedIn = true;
		m_KnowThisPerson.enable();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ie.dkit.semanticPhotoWeb.client.tools.LoginListener#loggedOut()
	 */
	public void loggedOut() {
		loggedIn = false;
		m_KnowThisPerson.disable();
	}
}
